'''
Created on Jun 19, 2012

@author: SG0214825
'''
import csv

class CSVFileDataSource():
    def __init__(self, file_path):
        self.rows = []
        self.lines = {}
        self.table = []
        with open(file_path, 'r') as f:
            reader = csv.reader(f, delimiter=',', quotechar='"')
            i = 0
            for row in reader:
                if i > 0: #Skip first row with metadata
                    value = None
                    key = None
                    timestamp = None
                    user = None
                    process = None
                    if len(row) == 3:
                        self.rows.append(float(row[2]))
                        value = float(row[2])
                        key = row[1]
                        timestamp = row[0]
                    elif len(row) == 5:
                        self.rows.append(float(row[4]))
                        value = float(row[4])
                        key = row[1] + ' filtered'
                        timestamp = row[0]
                        process = row[2]
                        user = row[3]
                    if not value is None :
                        if self.lines.has_key(key) :
                            if self.lines[key].has_key(timestamp):
                                self.lines[key][timestamp] += value
                                self.rows.append(self.lines[key][timestamp])
                            else:
                                self.lines[key][timestamp] = value 
                        else :
                            self.lines[key] = { timestamp : value}
                        self.table.append((timestamp, key, value, user, process))
                i = i + 1 